<?php
class ModelCatalogReview extends Model {
	public function addReview($data) {
	    $this->event->trigger('pre.review.add', $data);
	    $this->db->query("INSERT INTO " . DB_PREFIX . "review SET author = '" . $this->db->escape($this->customer->getFirstName()) . "', customer_id = '" . (int)$this->customer->getId() . "', product_id = '" . (int)$data['product_id'] . "', text = '" . $this->db->escape($data['text']) . "', rating = '" . (int)$data['rating'] . "', status = '1', date_added = NOW(),date_modified = NOW()");
	    $review_id = $this->db->getLastId();
	    $this->load->model('catalog/product');
	    $product_info = $this->model_catalog_product->getProduct($data['product_id']);
	    $this->event->trigger('post.review.add', $review_id);
	    return $review_id;
	    
	}

	public function getReviewsByProductId($product_id, $start = 0, $limit = 20) {
		if ($start < 0) {
			$start = 0;
		}

		if ($limit < 1) {
			$limit = 20;
		}

		$query = $this->db->query("SELECT r.review_id, r.author, r.rating, r.text, p.product_id, pd.name, p.price, p.image, r.date_added FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "product p ON (r.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND p.date_available <= NOW() AND p.status = '1' AND r.status = '1' ORDER BY r.date_added DESC LIMIT " . (int)$start . "," . (int)$limit);

		return $query->rows;
	}

	public function getTotalReviewsByProductId($product_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "product p ON (r.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND p.date_available <= NOW() AND p.status = '1' AND r.status = '1' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		return $query->row['total'];
	}
	
	public function getReviewsByOrderId($order_id) {
	    $query = $this->db->query("SELECT r.text,r.rating,r.date_added,c.firstname as author,c.image FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "customer c ON (r.customer_id = c.customer_id) WHERE r.order_id = '" . (int)$order_id . "' AND r.status = '1'");
	
	    return $query->row;
	}
	
	public function getReviewsByCustomerId($customer_id, $start = 0, $limit = 20) {
	    if ($start < 0) {
	        $start = 0;
	    }
	
	    if ($limit < 1) {
	        $limit = 20;
	    }
	
	    $query = $this->db->query("SELECT r.*,c.customer_id,c.firstname as author,c.image,c.image customer_image FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "customer c ON (r.customer_id = c.customer_id) WHERE r.product_customer_id='".$customer_id."' AND r.status = '1' ORDER BY r.date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
	
	    return $query->rows;
	}
	
	public function getTotalReviewsByCustomerId($customer_id) {
	    $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r WHERE r.product_customer_id='".$customer_id."' AND r.status = '1'");
	
	    return $query->row['total'];
	}
}